<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\common\controller\Login;
use think\Request;
use think\Db;
class Comment extends Login
{
    public function index()
    {
        
        $agent_id = session::get('agent_id');

        $data = Db::table('business_comment')
                    ->Field('c.id as cid,c.post_time as shangchuan,u.*,c.*,i.*')
                    ->alias('c')
                    ->join('business_users u','c.uid = u.id' ,'LEFT')
                    ->join('business_info i' ,'c.shop_id = i.id')
                    ->where('c.agent_id',$agent_id)
                    ->order('c.id desc')
                    ->paginate(10);


        $page = $data->render();
    
        $this->assign('page',$page);

        $this->assign('data',$data);

        return $this->fetch();

    }

    public function search()
    {
        $search = input('post.search');
         $agent_id = session::get('agent_id');

        $data = Db::table('business_comment')
                    ->Field('c.id as cid,c.post_time as shangchuan,u.*,c.*,i.*')
                    ->alias('c')
                    ->join('business_users u','c.uid = u.id' ,'LEFT')
                    ->join('business_info i' ,'c.shop_id = i.id')
                    ->where('i.business_name','like','%'.$search.'%')
                    ->where('c.agent_id',$agent_id)
                    ->order('c.id desc')
                    ->paginate(10);


        $page = $data->render();
    
        $this->assign('page',$page);

        $this->assign('data',$data);

        return $this->fetch('index');
    }

    public function reply($id)
    {
        $data = Db::table('business_comment')
                    ->Field('c.id as cid,c.post_time as shangchuan,u.*,c.*,i.*')
                    ->alias('c')
                    ->join('business_users u','c.uid = u.id' ,'LEFT')
                    ->join('business_info i' ,'c.shop_id = i.id')
                    ->order('c.id desc')
                    ->where('c.id',$id)
                    ->find();
        //var_dump($data);die;
        $this->assign('data',$data);

        return $this->fetch();  
    }

    public function do_reply()
    {
        $data = input('post.');
        $id = $data['id'];
        if (empty($data['comment_reply'])) {
            return $this->error('请回复有效内容！');
        }

        $data['static'] = 2;
       $static = db('comment')->update($data);

       if ($static) {
           return $this->redirect('admin/comment/index'); 
       }else{
            return $this->error('发生未知错误，请稍后重试！');
       }
    }

   

}
